package com.gskj.eduservice.controller;


import com.gskj.commonutils.ResultJson;
import com.gskj.eduservice.entity.EduChapter;
import com.gskj.eduservice.entity.chapter.ChapterVO;
import com.gskj.eduservice.service.IEduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-16
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private IEduChapterService iEduChapterService;

    /***
     *  根据 课程id 获取对应的 章节、小结集合
     * @param courseId
     * @return
     */
    @GetMapping("/get/chapter/video/{courseid}")
    public ResultJson getChapterVideo(
            @PathVariable(value = "courseid") String courseId) {
        //根据 课程id 获取对应的 章节、小结集合
        List<ChapterVO> list = iEduChapterService.getChapterVideoByCourseId(courseId);

        return ResultJson.success().data("chaperVideoList", list);
    }


    /***
     *  添加章节
     * @param chapter
     * @return
     */
    @PostMapping("/add/chapter")
    public ResultJson addChapter(
            @RequestBody EduChapter chapter) {

        iEduChapterService.save(chapter);
        return ResultJson.success();
    }

    /***
     *  根据id获取章节
     * @return
     */
    @GetMapping("/find/chapter/by/{id}")
    public ResultJson getChapterById(
            @PathVariable("id") String id) {
        EduChapter chapter = iEduChapterService.getById(id);
        return ResultJson.success().data("chapter", chapter);
    }

    /**
     * 修改章节
     *
     * @param chapter
     * @return
     */
    @PostMapping("/edit/chapter")
    public ResultJson editChapter(
            @RequestBody EduChapter chapter) {

        iEduChapterService.updateById(chapter);
        return ResultJson.success();
    }

    /***
     *  删除 章节
     * @param id
     * @return
     */
    @DeleteMapping("/remove/chapter/{id}")
    public ResultJson removeChapter(@PathVariable("id") String id) {

        // TODO 删除章节之前先判断 该章节下是否还有 小结。有小结不能删除
        boolean b = iEduChapterService.deleteChapter(id);

        if(b){
            return ResultJson.success();
        }else{
            return ResultJson.error();
        }
    }
}

